<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Settlement_m extends FOX_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function add_settlement($data){
        $this->db->insert('settlement',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return FALSE;
        }

    }
    public function get_settlement_list($limit,$start,$key){
        $this->db->select('settlement.*,project.project_id,project.project_name,users.user_uname,users.user_id');
        $this->db->from('settlement');
        $this->db->join('project','project.project_id = settlement.project_id','left');
        $this->db->join('users','users.user_id = settlement.user_id','left');
        $this->db->limit($start,$limit);
        if($key){
            $this->db->like('users.user_uname', $key);
        }
        $this->db->where('settlement.delete',0);
        $this->db->order_by('settlement_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function get_user_settlement_list($limit,$start,$key,$user_id){
        $this->db->select('settlement.*,project.project_id,project.project_name,users.user_uname,users.user_id');
        $this->db->from('settlement');
        $this->db->join('project','project.project_id = settlement.project_id','left');
        $this->db->join('users','users.user_id = settlement.user_id','left');
        $this->db->limit($start,$limit);
        if($key){
            $this->db->like('users.user_uname', $key);
        }
        $this->db->where('settlement.delete',0);
        $this->db->where('settlement.user_id',$user_id);
        $this->db->order_by('settlement_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function count_settlement_find($key=''){
        $this->db->select('settlement_id');
        $this->db->from('settlement');
        $this->db->join('users','users.user_id = settlement.user_id','left');
        if($key){
            $this->db->like('users.user_uname', $key);
        }
        $this->db->where('settlement.delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    public function update_settlement($settlement_id,$data){
        $this->db->where('settlement_id',$settlement_id);
        $this->db->update('settlement', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function get_one_settlement($settlement_id){
        $this->db->select('*');
        $this->db->from('settlement');
        $this->db->where('settlement_id',$settlement_id);
        $this->db->where('delete',0);
        $query = $this->db->get();
        $res = $query->row_array();
        return $res;
    }
    public function get_settlement_users(){
        $this->db->select('users.*,user_into.into_num');
        $this->db->from('users');
        $this->db->join('user_into','users.user_id = user_into.user_id','left');
        $this->db->where('users.delete',0);
        $this->db->order_by('users.user_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function count_settlement_monney($user_id,$project_id){
        $this->db->select_sum('settlement_money');
        $this->db->from('settlement');
        $this->db->where('user_id',$user_id);
        $this->db->where('project_id',$project_id);
        $query = $this->db->get();
        $result = $query->result();
        return ($result[0]->settlement_money)?$result[0]->settlement_money:'0.00';

    }
    public function get_project_settlement($user_id=''){
        $this->db->select('settlement.*,project.project_money,project.project_id');
        $this->db->from('settlement');
        $this->db->join('project','project.project_id = settlement.project_id','left');
        if($user_id){
            $this->db->where('settlement.user_id',$user_id);
        }
        $this->db->where('settlement.delete',0);
        $this->db->where('project.delete',0);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function update_project_settlement($project_id,$data){
        $this->db->where('project_id',$project_id);
        $this->db->update('settlement', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


}
